package com.hui.security.sys.controller;

import com.hui.security.common.response.ResultMapper;
import com.hui.security.common.response.ResultVO;
import com.hui.security.sys.entity.Permission;
import com.hui.security.sys.entity.Role;
import com.hui.security.sys.service.PermissionService;
import com.hui.security.sys.service.RoleService;
import com.hui.security.sys.vo.RoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <b><code>RoleController</code></b>
 * <p/>
 * Description:
 * <p/>
 * <b>Creation Time:</b> 2019/1/5 11:33.
 *
 * @author HuWeihui
 */
@RestController
@RequestMapping("/v1")
@Api(value = "角色模块-角色API", tags = "roles")
@Slf4j
public class RoleController {
    /**
     * The Role service.
     */
    @Autowired
    private RoleService roleService;

    /**
     * Create role result vo.
     *
     * @param roleVO        the role vo
     * @param bindingResult the binding result
     * @return the result vo
     * @author HuWeihui
     * @since hui_project v1
     */
    @ApiOperation(value = "创建角色", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping("/role")
    public ResultVO createRole(@RequestBody @Validated RoleVO roleVO, BindingResult bindingResult) {
        Role role = new Role();
        BeanUtils.copyProperties(roleVO, role);
        return ResultMapper.ok(roleService.add(role));
    }

    /**
     * Delete role result vo.
     *
     * @param roleId the role id
     * @return the result vo
     * @author HuWeihui
     * @since hui_project v1
     */
    @ApiOperation(value = "删除角色", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @DeleteMapping("/role/{id}")
    public ResultVO deleteRole(@PathVariable("id") String roleId) {
        roleService.delete(roleId);
        return ResultMapper.ok();
    }

    /**
     * Update role result vo.
     *
     * @param roleVO the role vo
     * @return the result vo
     * @author HuWeihui
     * @since hui_project v1
     */
    @ApiOperation(value = "更新角色", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PutMapping("/role")
    public ResultVO updateRole(@RequestBody @Validated RoleVO roleVO, BindingResult bindingResult) {
        Role role = new Role();
        BeanUtils.copyProperties(roleVO, role);
        roleService.update(role);
        return ResultMapper.ok();
    }

    /**
     * Gets role.
     *
     * @param roleId the role id
     * @return the role
     * @author HuWeihui
     * @since hui_project v1
     */
    @ApiOperation(value = "查询角色详情", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping("/role/{id}")
    public ResultVO getRole(@PathVariable("id") String roleId) {
        return ResultMapper.ok(roleService.get(roleId));
    }


    /**
     * List roles result vo.
     *
     * @param appId the app id
     * @return the result vo
     * @author : Hu weihui
     * @since hui_project v1
     */
    @ApiOperation(value = "分页查询角色", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping("/roles")
    public ResultVO listRoles(@RequestParam String appId) {
        return ResultMapper.ok();
    }

    /**
     * List roles by user id result vo.
     *
     * @param userId the user id
     * @return the result vo
     * @author HuWeihui
     * @since hui_project v1
     */
    @ApiOperation(value = "查询用户所属的所有角色", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping("/user/{id}/roles")
    public ResultVO listRolesByUser(@PathVariable("id") String userId) {
        List<Role> roleList = roleService.listByUserId(userId);
        return ResultMapper.ok(roleList);
    }
}
